package com.neusoft.servlet;

import com.alibaba.fastjson.JSON;
import com.neusoft.entity.TagsEntity;
import com.neusoft.mapper.CommentMapper;
import com.neusoft.mapper.TagMapper;
import com.neusoft.utils.SqlSessionFactoryUtils;
import com.neusoft.vo.Comment;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/api/articles/comments")
public class CommentServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        SqlSessionFactory sqlSessionFactory
                = SqlSessionFactoryUtils.getSqlSessionFactory();

        SqlSession sqlSession = sqlSessionFactory.openSession();
        CommentMapper mapper = sqlSession.getMapper(CommentMapper.class);
        List<Comment> allComments = mapper.getAllComments();
        sqlSession.close();

        String jsonStr = JSON.toJSONString(allComments);

        PrintWriter printWriter = resp.getWriter();
        printWriter.println(jsonStr);

    }
}
